package com.woke.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.http.HttpMethods;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.woke.adapter.BankCardsViewProvider;
import com.woke.adapter.OnItemClickListener;
import com.woke.data.BankCard;
import com.woke.data.EnumCardType;
import com.woke.method.MyApp;
import com.woke.method.NetworkUtils;
import com.woke.serizedatas.Datas_load;
import com.zhongcai.online.R;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 中交所 列表
 */
public class BankCardListFragment extends Fragment implements XRecyclerView.LoadingListener {

    private static final String TAG = ExchangeFragment.class.getSimpleName();
    private FrameLayout rootView;
    private XRecyclerView mRecyclerView;
    private List<BankCard> mBankCardList = new ArrayList<>();
    private int mType;
    private MultiTypeAdapter mMulitypeAdapter;
    private Items mItems;

    private MyApp application;
    private EnumCardType mEnumCardType;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mType = bundle.getInt("type");
        }
        mEnumCardType = EnumCardType.getCardTypeFromValue(mType);
        application = (MyApp) getActivity().getApplication();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = (FrameLayout) inflater
                    .inflate(R.layout.promote_list_empty, container, false);
            mRecyclerView = (XRecyclerView) rootView
                    .findViewById(R.id.promoteCodeList_recyclerView);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
            mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
            mRecyclerView.setLoadingListener(this);
            View mEmptyView = inflater.inflate(R.layout.empty_bankcardlist, null);
            TextView mEmptyPromptTv = (TextView) mEmptyView.findViewById(R.id.prompt_tv);
            if (mEnumCardType == EnumCardType.ACCOUNT_CARDS) {
                mEmptyPromptTv.setText("您还未标记一张银行卡为到账卡,是否立即标记!");
            } else if (mEnumCardType == EnumCardType.RECHARGE_CARDS) {
                mEmptyPromptTv.setText("您还未标记一张银行卡为充值卡,是否立即标记!");
            }
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            rootView.addView(mEmptyView, params);
            mRecyclerView.setEmptyView(mEmptyView);
            initMulitypeAdapter();
            initData();
        } else {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }

    private void initMulitypeAdapter() {

        mItems = new Items();
        mMulitypeAdapter = new MultiTypeAdapter();
        mMulitypeAdapter.register(BankCard.class, new BankCardsViewProvider(mItemClickListener));
        mRecyclerView.setAdapter(mMulitypeAdapter);
        mRecyclerView.setLoadingMoreEnabled(false);
        mMulitypeAdapter.setItems(mItems);
        mMulitypeAdapter.notifyDataSetChanged();
//		if (mItems.size () > 0) {
//			assertAllRegistered (mMulitypeAdapter, mItems);//执行这句话时,必须保证 Items.size()>0
//		}

    }

    private OnItemClickListener mItemClickListener = new OnItemClickListener() {
        @Override
        public void OnItemClick(View view, int position) {

        }

        @Override
        public void OnItemClick(View view, Object object, int position) {
            if (object instanceof BankCard) {
                BankCard bankCard = (BankCard) object;

            }

        }
    };


    private void initData() {
        Datas_load loaded = application.getDatas_load();
        if (null != loaded) {
            int userId = Integer.parseInt(loaded.getId());
            if (userId > 0) {
                if (mEnumCardType == EnumCardType.RECHARGE_CARDS || mEnumCardType == EnumCardType.ACCOUNT_CARDS) {
                    HttpMethods.getInstance().getBankCardList(mBankCardsObserver, userId, mEnumCardType.getValue());
                } else {
                    HttpMethods.getInstance().getBankCardList(mBankCardsObserver, userId);
                }
            }
        }
    }

    private Observer<List<BankCard>> mBankCardsObserver = new Observer<List<BankCard>>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(List<BankCard> value) {
            if (value != null && value.size() > 0) {
                mItems.clear();
                mItems.addAll(value);
                if (mMulitypeAdapter != null) {
                    mMulitypeAdapter.notifyDataSetChanged();
                }
            }
        }

        @Override
        public void onError(Throwable e) {
            if (mRecyclerView != null) {
                mRecyclerView.refreshComplete();
                mRecyclerView.loadMoreComplete();
            }
        }

        @Override
        public void onComplete() {
            if (mRecyclerView != null) {
                mRecyclerView.refreshComplete();
                mRecyclerView.loadMoreComplete();
            }
        }
    };


    @Override
    public void onRefresh() {
        if (getContext() != null) {
            if (NetworkUtils.isConnected(getActivity())) {
                initData();
            } else {
                mRecyclerView.refreshComplete();
            }
        }

    }

    @Override
    public void onLoadMore() {
        mRecyclerView.loadMoreComplete();
    }

}
